/**
 * 功能描述 导航
 */

define(function (require, exports, module) {
	var Component = require("subnav");
	var fetchProperties = require("../constant/fetchProperties")
	var aw = require("ajaxwrapper");
	var Dialog=require("dialog");
	
    var instance = {
        initComponent: function (params, widget) {
            var component = new Component({
            	parentNode:".J-subnav",
				model : {
					title:"产品定义",
					items : [{
						placeholder:"搜索(姓名)",
						id : "search",
						type : "search",
						handler : function(str) {
							var g = widget.get("grid");
							var param = {
									s:str,
		    						properties:"name",
		        					fetchProperties:"*,organization.*,communityDatas.*"
	        					};
							aw.ajax({
								url: "api/product/search",
								data:param,
								dataType:"json",
								success:function(data){
									g.setData(data);
								}
							});
						}
					},{
						id : "isSales",
						type:"buttongroup",
						items:[{
							key:"All",
							value:"全部"
						},{
							key:"true",
							value:"打开"
						},{
							key:"false",
							value:"关闭"
						}],
						handler:function(){
							widget.get("grid").refresh();
						}
					},{
						id : "add",
						type : "button",
						text : "新增",
						show : true,
						handler : function(){
							widget.get("form").reset();
							widget.get("editgrid").setData([]);
							widget.hide([".J-grid"]).show([".J-form",".J-editgrid"]);
							$(".J-button").removeClass("hidden")
							widget.get("subnav").hide(["isSales","search","add"]).show(["cancel","save"]);
						}
					},{
						id : "save",
						type : "button",
						text : "保存",
						show : false,
						handler : function(){
							var data=widget.get("form").getData();
							if(data.description.length>512){
								Dialog.alert({
			                		title:"提示",
			                		content:"只能输入512个字！"
			                	});
			            		return false;
							}
		            		 var r = /^(100|[1-9]\d|\d)$/;//0-100的正整数  		 
		        			 if(!r.test(data.checkInNum)){
		        				Dialog.alert({
			                		title:"提示",
			                		content:"入住适用人数只能输入非零正整数！"
			                	});
		        				widget.get("form").setValue("checkInNum","");
		        				return false;
		        			 }else{
		        				if(parseInt(data.checkInNum)==0){
		        					Dialog.alert({
				                		title:"提示",
				                		content:"入住适用人数只能输入非零正整数！"
				                	});
			        				widget.get("form").setValue("checkInNum","");
			        				return false;
		        				}
		        			 }
		        			 
		        			 if(!r.test(data.travelNum)){
			        				Dialog.alert({
				                		title:"提示",
				                		content:"旅居适用人数只能输入非零正整数！"
				                	});
			        				widget.get("form").setValue("travelNum","");
			        				return false;
			        			 }else{
			        				if(parseInt(data.travelNum)==0){
			        					Dialog.alert({
					                		title:"提示",
					                		content:"旅居适用人数只能输入非零正整数！"
					                	});
				        				widget.get("form").setValue("travelNum","");
				        				return false;
			        				}
			        			 }
							var dat=widget.get("editgrid").getData();
							var save={};
							save.product=data;
							save.communityDatas=data.communityDatas;
							var temp=[];
							for(var i=0;i<dat.length;i++){
								var pdSvcView={};
								pdSvcView.productDetail=dat[i]
								var tem1=[];
								for(var j in dat[i].expenseScope){
									tem1.push(dat[i].expenseScope[j].pkServicePoint);
								}
								pdSvcView.expenseScope=tem1.toString();
								var tem2=[];
								for(var j in dat[i].bedScope){
									tem2.push(dat[i].bedScope[j].pkServicePoint);
								}
								pdSvcView.bedScope=tem2.toString();
								var tem3=[];
								for(var j in dat[i].cashbackScope){
									tem3.push(dat[i].cashbackScope[j].pkServicePoint);
								}
								pdSvcView.cashbackScope=tem3.toString();
								var tem4=[];
								for(var j in dat[i].subsidyScope){
									tem4.push(dat[i].subsidyScope[j].pkServicePoint);
								}
								pdSvcView.subsidyScope=tem4.toString();
								temp.push(pdSvcView);
							}
							save.list=temp;
							save.pklist=widget.get("tem");
							aw.saveOrUpdate("api/productdetail/saveall",aw.customParam(save),function(data){
								tem=[];
								widget.get("grid").refresh();
								widget.show([".J-grid"]).hide([".J-form",".J-editgrid"]);
								widget.get("subnav").show(["isSales","search","add"]).hide(["cancel","save"]);
								$(".J-button").addClass("hidden");
		      				});
						}
					},{
						id : "cancel",
						type : "button",
						text : "返回",
						show : false,
						handler : function(){
							widget.show([".J-grid"]).hide([".J-form",".J-editgrid"]);
							widget.get("subnav").show(["isSales","search","add"]).hide(["cancel","save"]);
							$(".J-button").addClass("hidden");
						}
//					},{
//						id : "return",
//						type : "button",
//						text : "返回",
//						show : false,
//						handler : function(){
//							 widget.hide(".J-editform").show([".J-form",".J-editgrid"]);
//		        	         widget.get("subnav").show(["cancel","save"]).hide(["return"]);
//		        	         $(".J-button").removeClass("hidden");
//						}
					}]
				}
            });
            widget.set("subnav", component);
        }
    };
    module.exports = instance;
});
